﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


public partial class UserControl_ControlXacMinhDuLieuImport : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            txtFromDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            LoadGrid();
        }
       
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        LoadGrid();
    }
    protected void grvIntxd_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        grvIntxd.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
    void LoadGrid()
    {
       
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db=null;
            string YyyyMm = DateTime.Now.ToString("yyyyMM");
            if (chkFromDateToDate.Checked)
            {

                db = dt.Get_Data_DataTable(str, ref error, "[k_getFileImportedWithDate]", CommandType.StoredProcedure,
                     new SqlParameter("@Distributor", drlDistributor.SelectedValue.ToString()),
                     new SqlParameter("@From", txtFromDate.Text),
                     new SqlParameter("@YyyyMm",YyyyMm));
                    
            }
            else
            {
                db = dt.Get_Data_DataTable(str, ref error, "k_getFileImported", CommandType.StoredProcedure,
                    new SqlParameter("@Distributor", drlDistributor.SelectedValue.ToString()),
                    new SqlParameter("@YyyyMm",YyyyMm));
            }
           
            if (db.Rows.Count> 0)
            {
                grvIntxd.DataSource = db;
                
            }
            else
            {
                grvIntxd.DataSource = null;
            }
            grvIntxd.DataBind();
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    protected void grvIntxd_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        Label lblFileNameDestination = (Label)grvIntxd.Rows[e.RowIndex].FindControl("lblFileNameDestination");
        string YyyyMm = DateTime.Now.ToString("yyyyMM");
        dt = new ClassExecute.ClassExecute();
        try
        {
            //xóa dữ liệu trong database
            bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_deleteInvoiceDetail", ref error,
                           new SqlParameter("@Username",Session["user"].ToString()),
                           new SqlParameter("@Filename", lblFileNameDestination.Text),
                           new SqlParameter("@YyyyMm",YyyyMm));
            //xóa file trong server
            //try
            //{
            //    string filePathSource = webapp4U.Globals.PhysicalApplicationPath() + @"/UserUpload/ImportedFileExcel/" + YyyyMm + "/" + lblFileNameDestination.Text;
            //    File.Delete(filePathSource);
            //}
            //catch
            //{
            //    return;
            //}
            ShowMessageBox("Đã xóa dữ liệu thành công");
            LoadGrid();

        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }

    }
    protected void grvIntxd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {

                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                string YyyyMm = DateTime.Now.ToString("yyyyMM");
                Label lblFileNameDestination = (Label)grv.FindControl("lblFileNameDestination");
               

                //xóa dữ liệu trong database
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_deleteInvoiceDetail", ref error,
                           new SqlParameter("@Username", Session["user"].ToString()),
                           new SqlParameter("@Filename", lblFileNameDestination.Text),
                           new SqlParameter("@YyyyMm", YyyyMm));
                //xóa file trong server
                if (kq)
                {
                    ShowMessageBox("Đã xóa dữ liệu thành công");
                    LoadGrid();
                }
                else
                {
                    ShowMessageBox("Hệ thống đang bảo trì");
                }


            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    
}